<?php

if (isset($mode) && ($mode == "login")) {
    $action_url = array('controller' => 'applet', 'action' => 'go', 'auth' => $auth);
    ?><?=$editForm->create($modelName, array('url' => $action_url));?><?php
        ?><?=$editForm->startSet();?><?php
            ?><?=$editForm->field('pass', __d(null, 'password', true), $form->password('pass', array('size'=>30)), array('vertical' => true));?><?php
            ?><?=$editForm->cell($editForm->submit(__d(null, 'submit', true)));?><?php
        ?><?=$editForm->endSet();?><?php
    ?><?=$editForm->end();?><?php
} elseif (isset($mode) && ($mode == "completed")) {
    ?><h2><?=$html->link(__d('auth', 'enter', true), '/login');?></h2><?php
} else {
    vendor('json');

    $msg_not_java = __d(null, 'Your browser does not have a Java-plug.', true);
    $msg_download_link = sprintf(__d(null, 'Please <a href="%s">download</a> the latest version of the plug.', true), "http://java.sun.com/products/plugin/downloads/index.html");
    $start_tool_url = $html->url(array('controller' => 'testings', 'action' => 'questionnaireStart'), true);
    $end_tool_url = $html->url(array('controller' => 'testings', 'action' => 'questionnaireEnd'), true);

    $applet_env = array(
        "startToolUrl" => $start_tool_url,
        "endToolUrl" => $end_tool_url,
        "userName" => $username,
        "auth" => $auth,
        "po" => array(
            "noResponseFromServer" => __d(null, 'No response from server. Test cannot start', true),
        ),
    );
    $applet_data = array(
        "id" => "jsap",
        "width" => 950,
        "height" => 600,
        "url" => $html->url('/applets/'.$testing_hash.'.jar?time='.time(), true),
        "className" => $applet_class_name,
        "unloadContent" => "<p>{$msg_not_java}{$msg_download_link}</p>",
    );

    ?><head><?php
        ?><title><?=h($page_title);?></title><?php
        ?><?=$html->css("style", null, array());?><?php
        ?><?=$javascript->link('jquery-1.2.2.pack');?><?php
        ?><?=$javascript->link('main');?><?php
        ?><?=$javascript->link('applet');?><?php
    ?></head><?php
    ?><body style="background-image: none;"><?php
        ?><noscript><?php
            ?><p><?=__d(null, "noscript_msg", true);?></p><?php
        ?></noscript><?php
        ?><div id="jsap"><span></span></div><?php
        ?><?=$javascript->codeBlock('
            var appletToolObj = new appletTool(' . json::drawToComment($applet_env) . ');

            function windowCloseMessage()
            {
                return '.json::drawToComment(__d(null, "close_testing_window_msg", true)).';
            }

            function startTool()
            {
                return appletToolObj.startTool();
            }

            function getUserName()
            {
                appletToolObj.init();
                return appletToolObj.getUserName();
            }

            function setResult(str)
            {
                var result = appletToolObj.setResult(str);
                '.(! empty($redirect_to_home) ?
                'if (result == \'true\') {
                    document.location.href = "'.htmlspecialchars(addslashes(Application::rootUrlPath()), ENT_NOQUOTES).'/";
                }
                ' : '').
                'return result;
            }

            var applets = ' . json::drawToComment(array($applet_data)) . ';
        ');?><?php
        ?><?=$javascript->link('applets');?><?php
    ?></body><?php
}

?>